package driver.dao;

import driver.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class SearchUserDao {
    //String sql="select * from user where school_num=? and name=? or phonenum=? or idcardnum=?";
    public ArrayList<User> search(String str,String school_num) throws SQLException {

        Connection conn = new BaseDao().getConn();
        PreparedStatement prep;
        if(school_num.equals(""))
        {
            prep = conn.prepareStatement("select * from user where  phonenum=? or idcardnum=? or name=?;");
            prep.setObject(1,str);
            prep.setObject(2,str);
            prep.setString(3,str);
        }
        else
        {
        prep = conn.prepareStatement("select * from user where school_num=? and( phonenum=? or idcardnum=? or name=?);");
        prep.setObject(1,school_num);
        prep.setObject(2,str);
        prep.setObject(3,str);
        prep.setString(4,str);
        }
        ResultSet result = prep.executeQuery();
        ArrayList<User> list=new ArrayList<>();
        while(result.next())
        {
            User user=new User();
            user.setId(result.getInt(1));
            user.setName(result.getString(2));
            user.setPhonenum(result.getString(3));
            user.setIdcardnum(result.getString(4));
            user.setPolicyid(result.getString(5));
            user.setPolicyprod(result.getString(6));
            user.setPolicytype(result.getString(7));
            user.setPolicynum(result.getInt(8));
            //user.setSchool_num(user.getSchool_num());
            user.setStarttime(result.getDate(10));
            user.setEndtime(result.getDate(11));
            list.add(user);

        }
        JDBCUtils.close(result,prep,conn);


        return list;
    }

   /* public static void main(String[] args) throws SQLException{
        ArrayList<User> search = new SearchUserDao().search("19829088780", "18292761793");
        System.out.println(search.toString());
    }*/
}
